import * as vscode from 'vscode';
import { SceneParser } from './sceneParser';
import { TerminalManager } from './terminalManager';
import { WebviewManager } from './webviewManager';
import { getPythonPath, getManimCommand } from './utils';

export function activate(context: vscode.ExtensionContext) {
    const sceneParser = new SceneParser();
    const terminalManager = new TerminalManager();
    const webviewManager = new WebviewManager(context);

    // 注册运行Manim场景的命令
    let disposable = vscode.commands.registerCommand('manim-runner.runScene', async () => {
        const editor = vscode.window.activeTextEditor;
        if (!editor || editor.document.languageId !== 'python') {
            vscode.window.showErrorMessage('请打开一个Python文件');
            return;
        }

        const filePath = editor.document.uri.fsPath;
        const scenes = sceneParser.parseScenes(filePath);
        if (scenes.length === 0) {
            vscode.window.showErrorMessage('未找到任何Scene类');
            return;
        }

        // 让用户选择要渲染的场景
        const sceneName = await vscode.window.showQuickPick(scenes, { placeHolder: '选择要渲染的Scene' });
        if (!sceneName) return;

        // 获取配置中的默认清晰度
        const config = vscode.workspace.getConfiguration('manim-runner');
        const quality = config.get('defaultQuality', 'l');
        const pythonPath = getPythonPath();
        const command = getManimCommand(pythonPath, filePath, sceneName, quality);

        // 在终端中运行命令并在完成后更新预览
        terminalManager.runCommand(command, (outputFile) => {
            const renderTime = 30; // 这里是模拟值，实际应动态计算
            webviewManager.updatePreview(outputFile, renderTime, quality, sceneName, filePath);
        });
    });

    context.subscriptions.push(disposable);
}

export function deactivate() {
    // 清理资源（如果有）
}